package com.ln.asset.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.ln.asset.core.config.LendNoUtils;
import com.ln.asset.core.mapper.AssetInfoMapper;
import com.ln.asset.core.mapper.TransformationMapper;
import com.ln.asset.core.pojo.AssetInfo;
import com.ln.asset.core.pojo.Transformation;
import com.ln.asset.core.pojo.vo.TransforDetailsVO;
import com.ln.asset.core.service.TransformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiaoyanru
 * @since 2022-04-27
 */
@Service
public class TransformationServiceImpl extends ServiceImpl<TransformationMapper, Transformation> implements TransformationService {
    @Resource
    private TransformationMapper transformationMapper;
    @Resource
    private AssetInfoMapper assetInfoMapper;

    /**
     * 转移资产信息
     * @return
     */
    @Override
    public IPage<Transformation> selectList(Page<Transformation> pageParam) {
        Page<Transformation> page = transformationMapper.selectPage(pageParam, null);
        return page;
    }

    /**
     * 删除转移信息
     * @param id
     * @return
     */
    @Override
    public void delTransfor(Integer id) {
        transformationMapper.deleteById(id);
    }

    /**
     * 申请转移
     * @param transformation
     */
    @Override
    public void applyTransfor(Transformation transformation) {
        transformation.setTNo(LendNoUtils.getTransforNo());
        transformation.setTStatus(2);
        transformation.setIsDeleted(0);
        baseMapper.insert(transformation);
    }

    /**
     * 转移资产详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> selectDetails(Integer id) {

        //根据转移资产id查询转移资产信息
        Transformation transformation = baseMapper.selectById(id);

        //根据转移资产id查询资产信息
        List<TransforDetailsVO> transforDetailsVO = transformationMapper.selectAssetByTransid(id);

        Map<String,Object> map = new HashMap<>();
        map.put("transformation",transformation);
        map.put("transforDetailsVO",transforDetailsVO);

        return map;
    }

    /**
     * 资产转移审批通过
     * @param status
     * @param transforDetails
     */
    @Override
    public void transforApproval(Integer status, Map<String, Object> transforDetails) {

        //map取对象和集合
        ObjectMapper objectMapper = new ObjectMapper();
        //对象
        objectMapper.registerModule(new JavaTimeModule());
        Transformation transformation = objectMapper.convertValue(transforDetails.get("transformation"), Transformation.class);
        System.out.println("transformation对象======"+transformation);

        //集合
        List<TransforDetailsVO> list = new ArrayList<>();
        List transforDetailsVO = objectMapper.convertValue(transforDetails.get("transforDetailsVO"), list.getClass());

        if (status!=null && status==0){
            transformation.setTStatus(1);
            baseMapper.updateById(transformation);
            if (transforDetailsVO.size()>0){
                for (int i = 0 ; i < transforDetailsVO.size(); i++){
                    TransforDetailsVO transformation1 = JSON.parseObject(JSON.toJSONString(transforDetailsVO.get(i)), new TypeReference<TransforDetailsVO>() {});
                    QueryWrapper<AssetInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("a_no",transformation1.getANo());
                    AssetInfo assetInfo = assetInfoMapper.selectOne(queryWrapper);
                    assetInfo.setACount(assetInfo.getACount()-1);
                    assetInfoMapper.updateById(assetInfo);
                }
            }
        }else if(status==1) {
            transformation.setTStatus(0);
            baseMapper.updateById(transformation);
        }
    }

    @Override
    public void applyTransforZhong(Integer tId, List<Integer> list) {
        transformationMapper.applyTransforZhong(tId,list);
    }

}
